package org.mercworks.igamexmlparser;

import java.io.InputStream;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.mercworks.igamexmlparser.dvo.IGameModel;

public class IGameXmlParser
{
	private String xmlFileName;
	private IGameXmlParserHandler iGameXmlParserHandler;
	
	public IGameXmlParser(String xmlFileName)
	{
		this.xmlFileName = xmlFileName;
		this.iGameXmlParserHandler = new IGameXmlParserHandler();
	}
	
	public IGameModel getXmlObject()
	{
		SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
		
		try
		{
			InputStream xmlInputStream = this.getClass().getClassLoader().getResourceAsStream("resources/xml/" + this.xmlFileName);
			SAXParser saxParser = saxParserFactory.newSAXParser();
			saxParser.parse(xmlInputStream, this.iGameXmlParserHandler);
			xmlInputStream.close();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		
		return this.iGameXmlParserHandler.getIGame();
	}
}
